from datetime import datetime

from django.http import JsonResponse
from django.shortcuts import render
from app01.models import Game, UserGameScore
from app01.utils.pagination import Pagination


# Create your views here.


def game_read(request, id):
    coo = request.session.get('info', "")
    if request.method == 'GET':
        read_game = Game.objects.filter(id=id).first()
        return render(request, "game/game.html", {"read_game": read_game, "coo": coo})


def game_show(request, type):
    coo = request.session.get('info', "")
    if request.method == 'GET':
        show_game = Game.objects.all()
        page_object = Pagination(request, show_game)
        context = {
            'coo': coo,
            "show_game": page_object.page_queryset,
            "page_string": page_object.html()
        }
        return render(request, "game/game_show.html", context)


def game_score(request):
    coo = request.session.get('info', "")
    score = request.GET.get('score')
    uid = request.GET.get('uid')
    exists = UserGameScore.objects.filter(user_id_id=coo["id"], game_id_id=uid).exists()
    if not exists:
        UserGameScore.objects.create(user_id_id=coo["id"], game_id_id=uid, score=score, time=datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
        return JsonResponse({"status": True})
    return JsonResponse({'status': False, 'error': '您已评价过了哦'})

